//
//  chaxunpeixunViewController.m
//  PASystem
//
//  Created by michael on 16/10/11.
//  Copyright © 2016年 czd. All rights reserved.
//

#import "chaxunpeixunViewController.h"
#import "MHDatePicker.h"
#import "huiyiguanliTableViewController.h"
@interface chaxunpeixunViewController ()
@property (strong, nonatomic) MHDatePicker *selectDatePicker;

@property (strong, nonatomic)  UITextView *nameField;
@property (strong, nonatomic)  UIButton *button1;
@property (strong, nonatomic)  UIButton *button2;

@end

@implementation chaxunpeixunViewController

- (void)viewDidLoad {
    [super viewDidLoad];
self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0,0, KScreenWidth,KScreenHeight-46-64 ) style:UITableViewStylePlain];
_tableview.dataSource = self;
_tableview.delegate = self;
[self.view addSubview:_tableview];

UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.frame = CGRectMake(0, 0, 44, 44);
[rightButton addTarget:self action:@selector(rightButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[rightButton setTitle:@"查询" forState:UIControlStateNormal];
[rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                 
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];


 //点击手势
 UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(keybordDown)];
    [self.view addGestureRecognizer:tap];

}
- (void)rightButtonAction:(UIButton *)button {

    huiyiguanliTableViewController *guowai = [[huiyiguanliTableViewController alloc]init];
        guowai.type = _type;
guowai.string = _string;
 guowai.action = _action;
        guowai.service = _service;
    guowai.timeafterstring =_timeafterstring;
    guowai.timebeforestring =_timebeforestring;
   if (_nameField.text.length ==0) {
    guowai.name = nil;
}
    else{
    guowai.name = _nameField.text;}
    
    [self.navigationController pushViewController: guowai  animated:YES];



}
- (void)keybordDown {
       [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
                return 1;
 }
 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//if (indexPath.row==0) {
//
//     UITableViewCell* cell = [[UITableViewCell alloc]init];
//      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, cell.contentView.height)];
//    label.text = [NSString stringWithFormat:@"%@主题",_string];
//    
//    label.font = [UIFont systemFontOfSize:14];
//    [cell.contentView addSubview:label];
//      _nameField = [[UITextView alloc]init];
//      _nameField.frame = CGRectMake(label.right, label.top+5, KScreenWidth-120, cell.height-10);
////      UIColor *myColor = [UIColor colorWithWhite:0.5 alpha:0.2];
////      _nameField.backgroundColor = myColor;
// [_nameField setFont:[UIFont systemFontOfSize:13]];
// _nameField.textAlignment = NSTextAlignmentLeft;
//      _nameField.delegate = self;
//      _nameField.layer.borderColor = UIColor.blackColor.CGColor;_nameField.layer.borderWidth = 1;
//       [cell.contentView addSubview:_nameField];
//             return cell;
//
//}
//else{
//
     UITableViewCell* cell = [[UITableViewCell alloc]init];
      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, cell.contentView.height)];
      label.numberOfLines = 0;
    label.text = @"起止时间 :";
    
    label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
    _button1 = [UIButton buttonWithType:UIButtonTypeCustom];
      _button1.frame = CGRectMake(label.right, label.top, 100, label.height);
//    _button1.backgroundColor = [UIColor yellowColor];
      [_button1 setTitle:@"开始时间" forState:UIControlStateNormal];
    _button1.titleLabel.font = [UIFont systemFontOfSize: 12.0];
    
       _button2 = [UIButton buttonWithType:UIButtonTypeCustom];
      _button2.frame = CGRectMake(_button1.right, label.top, 110, label.height);
      [_button2 setTitle:@"结束时间" forState:UIControlStateNormal];
          _button2.titleLabel.font = [UIFont systemFontOfSize: 12.0];
      [_button1 setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
      [_button2 setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
      [_button1 addTarget:self action:@selector(action1) forControlEvents:UIControlEventTouchUpInside];
[_button2 addTarget:self action:@selector(action2) forControlEvents:UIControlEventTouchUpInside];
       [cell.contentView addSubview:_button1];
       [cell.contentView addSubview:_button2];
             return cell;



//}



}
- (void)action1{
 _selectDatePicker = [[MHDatePicker alloc] init];
    _selectDatePicker.isBeforeTime = YES;
    _selectDatePicker.datePickerMode = UIDatePickerModeDate;
    
    __weak typeof(self) weakSelf = self;
    [_selectDatePicker didFinishSelectedDate:^(NSDate *selectedDate) {
//        NSString *string = [NSString stringWithFormat:@"%@",[NSDate dateWithTimeInterval:3600*8 sinceDate:selectedDate]];
//        weakSelf.myLabel2.text = string;
      NSString *string = [weakSelf dateStringWithDate:selectedDate DateFormat:@"yyyy-MM-dd"];
      _timebeforestring = string;
      [weakSelf.button1 setTitle:string forState:UIControlStateNormal];
    }];

}
- (void)action2{
_selectDatePicker = [[MHDatePicker alloc] init];
    _selectDatePicker.isBeforeTime = YES;
    _selectDatePicker.datePickerMode = UIDatePickerModeDate;
    
    __weak typeof(self) weakSelf = self;
    [_selectDatePicker didFinishSelectedDate:^(NSDate *selectedDate) {
//        NSString *string = [NSString stringWithFormat:@"%@",[NSDate dateWithTimeInterval:3600*8 sinceDate:selectedDate]];
//        weakSelf.myLabel2.text = string;
      NSString *string = [weakSelf dateStringWithDate:selectedDate DateFormat:@"yyyy-MM-dd"];
        _timeafterstring = string;
      [weakSelf.button2 setTitle:[NSString stringWithFormat:@"%@",string] forState:UIControlStateNormal];
    }];

}

- (NSString *)dateStringWithDate:(NSDate *)date DateFormat:(NSString *)dateFormat
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:dateFormat];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
    NSString *str = [dateFormatter stringFromDate:date];
    return str ? str : @"";
}
@end
